package com.junit;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hcfly.entity.Person;
import com.hcfly.utils.ESClient;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OperatorDoc {

    ObjectMapper mapper = new ObjectMapper();

    private static final RestHighLevelClient client = ESClient.getClient();
    private static final String index = "person";
    private static final String type  = "man";

    @Test
    public void BulkDelete() throws IOException {
        BulkRequest request = new BulkRequest();
        request.add(new DeleteRequest(index, type, "1"));
        request.add(new DeleteRequest(index, type, "2"));
        request.add(new DeleteRequest(index, type, "3"));

        BulkResponse res = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(res.toString());

    }


    @Test
    public void BulkOpeartor() throws IOException {
        /**
         * 批量操作
         */
        Person p1 = new Person(1, "张三", 15, new Date());
        Person p2 = new Person(2, "李四", 19, new Date());
        Person p3 = new Person(3, "王五", 22, new Date());

        String json1 = mapper.writeValueAsString(p1);
        String json2 = mapper.writeValueAsString(p2);
        String json3 = mapper.writeValueAsString(p3);

        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest(index, type, p1.getId().toString()).source(json1, XContentType.JSON));
        request.add(new IndexRequest(index, type, p2.getId().toString()).source(json2, XContentType.JSON));
        request.add(new IndexRequest(index, type, p3.getId().toString()).source(json3, XContentType.JSON));

        BulkResponse res = client.bulk(request, RequestOptions.DEFAULT);
        System.out.println(res.toString());

    }


    @Test
    public void deleteDoc() throws IOException {
        DeleteRequest request = new DeleteRequest(index, type, "1");
        DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(delete.getResult().toString());
    }

    @Test
    public void updateDoc() throws IOException {
        // 创建一个常量
        Map<String, Object> doc = new HashMap<String, Object>();
        doc.put("name", "张大三");
        String docId = "1";

        // 创建request对象，进行封装
        UpdateRequest request = new UpdateRequest(index, type, docId);
        request.doc(doc);

        // 执行
        UpdateResponse update = client.update(request, RequestOptions.DEFAULT);

        System.out.println(update.getResult().toString());

    }

    @Test
    public void createDoc() throws IOException {
        /**
         * 创建一个文档
         */
        // 准备一个json数据
        Person person = new Person(1, "张三", 15, new Date());
        String json = mapper.writeValueAsString(person);

        // 准备一个request对象
        IndexRequest req = new IndexRequest(index, type, person.getId().toString());
        req.source(json, XContentType.JSON);

        // 通过client添加操作
        IndexResponse res = client.index(req, RequestOptions.DEFAULT);

        System.out.println(res.getResult().toString());




    }


}
